import apiClient from '../apiClient'

export interface KnowledgeItem {
  id: number
  title: string
  content: string
  author: string
  tags?: string[]
  views: number
  likes: number
  createTime: string
}

export interface KnowledgeListParams {
  page: number
  pageSize: number
  keyword?: string
}

export interface KnowledgeListResponse {
  list: KnowledgeItem[]
  total: number
}

export enum KnowledgeApi {
  LIST = '/knowledge/list',
  DETAIL = '/knowledge',
  CREATE = '/knowledge',
  UPDATE = '/knowledge',
  DELETE = '/knowledge',
}

const getKnowledgeList = (params: KnowledgeListParams) => 
  apiClient.get<KnowledgeListResponse>({ 
    url: KnowledgeApi.LIST, 
    params 
  })

const getKnowledgeDetail = (id: number) => 
  apiClient.get<KnowledgeItem>({ 
    url: `${KnowledgeApi.DETAIL}/${id}` 
  })

const createKnowledge = (data: Omit<KnowledgeItem, 'id' | 'views' | 'likes' | 'createTime'>) => 
  apiClient.post({ 
    url: KnowledgeApi.CREATE, 
    data 
  })

const updateKnowledge = (id: number, data: Partial<KnowledgeItem>) => 
  apiClient.put({ 
    url: `${KnowledgeApi.UPDATE}/${id}`, 
    data 
  })

const deleteKnowledge = (id: number) => 
  apiClient.delete({ 
    url: `${KnowledgeApi.DELETE}/${id}` 
  })

export default {
  getKnowledgeList,
  getKnowledgeDetail,
  createKnowledge,
  updateKnowledge,
  deleteKnowledge,
}
